import type { MessageOptions } from 'element-plus-cisdi';
import { FeCode, getCodeMsg } from '@lingshu/request-lib';
import { ElMessage } from 'element-plus-cisdi';

// 返回解析错误信息
export function transResponseData(res: any) {
  const { code, message, feMessage } = res || {};
  if (Object.values(FeCode).includes(`${code}`.toUpperCase())) {
    return { code, message: getCodeMsg(`${code}`.toUpperCase()) };
  }
  return {
    code,
    message: feMessage || message || getCodeMsg(FeCode.OUT_UNKNOWN_ERROR),
  };
}

// 错误提示信息
/**
 *
 * @param res 网络请求错误响应
 * @param showMsg 是否进行错误提示
 * @param messageOptions ElMessage提示参数(具有默认配置)
 * @returns { code: any; message: any; } 返回解析错误信息
 */
export function waringTip(res: any, showMsg = true, messageOptions?: MessageOptions) {
  const temp = transResponseData(res);
  if (showMsg) {
    const msgOptions: MessageOptions = {
      message: temp.message,
      plain: true,
      type: 'error',
      ...(messageOptions || {}),
    };
    ElMessage(msgOptions);
  }
  return temp;
}
